<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Request;
use think\Config;

class Zhuanyeshukan extends Frontend
{

    protected $noNeedLogin = ['index','qikan_detail','qikanqishu_detail','qikanwenzhang_detail','shuji_detail'];
    protected $noNeedRight = '*';
    protected $layout = 'default';

    public function index()
    {
		$this->view->assign('title', __('专业书刊'));

		$banner=Db::name('banner')->where([
			'type_data'=>0,
			'bannertype_id'=>21
		])->select();
		$this->assign('banner',$banner);




		$rs_qikan1=Db::name('qikan')->where('name','橡塑技术与装备')->find();
		$this->assign('rs_qikan1',$rs_qikan1);

		$rs_qikan=Db::name('qikan')->where([
		    'id'=>['<>',$rs_qikan1['id']],
		    'show_switch'=>1,
		])->order('weigh desc')->limit(10)->select();
		$this->assign('rs_qikan',$rs_qikan);

		$rs_shuji=Db::name('shuji')->order('weigh desc')->limit(10)->select();
		$this->assign('rs_shuji',$rs_shuji);

        return $this->view->fetch();
    }

	public function qikan_tougao()
	{
		$this->view->assign('title', __('在线投稿'));
		$banner=Db::name('banner')->where([
			'type_data'=>0,
			'bannertype_id'=>17
		])->select();
		$this->assign('banner',$banner);


	    return $this->view->fetch();
	}
	public function tougao_result(Request $request){

		$this->view->assign('title', __('在线投稿-提交成功'));
		return $this->view->fetch();
	}

	public function qikan_dingyue(Request $request)
	{
		$this->view->assign('title', __('期刊订阅'));
		$id=$request->param('id/d',0);
		$rs=Db::name('qikan')->where(['id'=>$id])->find();

		if(is_null($rs)){
			$this->error('内容不存在');
		}
		$this->assign('rs',$rs);

	    return $this->view->fetch();
	}

	public function qikan_dingyue_fukuan(Request $request){
		$this->view->engine->layout(false);
		$id=$request->param('id/d',0);
		$rs=Db::name('qikandingyue')->where(['id'=>$id,'user_id'=>$this->auth->id])->find();

		if(is_null($rs)){
			$this->error('订单不存在');
		}

		$this->assign('rs',$rs);




		return $this->view->fetch();
	}

	public function qikan_dingyue_result(Request $request){

		return $this->view->fetch();
	}

	public function qikan_detail(Request $request)
	{
		$this->view->assign('title', __('期刊详情'));
		$id=$request->param('id/d',0);
		$rs=Db::name('qikan')->where(['id'=>$id])->find();

		if(is_null($rs)){
			$this->error('内容不存在');
		}
		$this->assign('rs',$rs);

		$years=Db::name('qikanqishu')->distinct(true)->where('qikan_id',$id)->order('year desc')->column('year');

		$this->assign('years',$years);

		if(empty($years)){
			$this->error('该期刊未创建期数');
		}

		if($request->has('year')){
			$rs_qikan=Db::name('qikanqishu')->where([
				'qikan_id'=>$id,
				'year'=>$request->param('year')
			])->order('weigh desc')->select();
		}else{
			$rs_qikan=Db::name('qikanqishu')->where([
				'qikan_id'=>$id,
				'year'=>$years[0]
			])->order('weigh desc')->select();
		}

		$this->assign('year',$request->has('year')?$request->param('year'):$years[0]);

		$this->assign('rs_qikan',$rs_qikan);

	    return $this->view->fetch();
	}

	public function qikanqishu_detail(Request $request){
		$id=$request->param('id/d',0);
		$rs=Db::name('qikanqishu')->alias('s')->where(['s.id'=>$id])->field('s.*,q.name as qikan_name')
		->join('qikan q','s.qikan_id=q.id','LEFT')->find();

		$this->assign('rs',$rs);

		$rs_type=Db::name('qikanwenzhangtype')->where([
            'qikan_id'=>$rs['qikan_id'],
            'qikanqishu_id'=>$id,
            'show_switch'=>1
        ])->order('weigh desc')->select();
		foreach($rs_type as $k=>$v){
			$rs_type[$k]['docs']=Db::name('qikanwenzhang')->where([
				'qikanwenzhangtype_id'=>$v['id'],
				'qikanqishu_id'=>$id,
				'qikan_id'=>$v['qikan_id'],
			])->order('weigh desc')->select();
		}
		$this->assign('rs_type',$rs_type);
		$this->view->assign('title', __('专业书刊-期刊期数-期数介绍'));
		return $this->view->fetch();
	}

	public function qikanwenzhang_detail(Request $request){
		$id=$request->param('id/d',0);
		$rs=Db::name('qikanwenzhang')->alias('a')->where(['a.id'=>$id])->field('a.*,b.image')
		->join('qikan b','a.qikan_id=b.id','LEFT')->find();

		$this->assign('rs',$rs);

		$this->view->assign('title', __('期刊详情-期数介绍-文章详情'));
		return $this->view->fetch();
	}

	public function shuji_detail(Request $request){
		$id=$request->param('id/d',0);
		$rs=Db::name('shuji')->where(['id'=>$id])->find();

		$this->assign('rs',$rs);

		$this->view->assign('title', __('专业书刊-书籍详情'));
		return $this->view->fetch();
	}
}
